package com.wwwarehouse.usercenter.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.response.InviteInfoResponseBean;

import java.util.List;

public class InviteDetailsAdapter extends BaseAdapter {
    private Context mContext;
    private List<InviteInfoResponseBean.InviteDetailsBean> mInviteDetailsList;
    private int mItemHeight;

    public InviteDetailsAdapter(Context context, List<InviteInfoResponseBean.InviteDetailsBean> inviteDetailsList, int itemHeight) {
        this.mContext = context;
        this.mInviteDetailsList = inviteDetailsList;
        this.mItemHeight = itemHeight;
    }

    @Override
    public int getCount() {
        return mInviteDetailsList == null ? 0 : mInviteDetailsList.size();
    }

    @Override
    public Object getItem(int position) {
        return mInviteDetailsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        InviteInfoResponseBean.InviteDetailsBean inviteDetailBean = mInviteDetailsList.get(position);
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_invite_details, null);
            holder = new ViewHolder();
            holder.rootLayout = (LinearLayout) convertView.findViewById(R.id.ll_root);
            holder.faceImg = (ImageView) convertView.findViewById(R.id.iv_face);
            holder.statusTxt = (TextView) convertView.findViewById(R.id.tv_status);
            holder.nameTxt = (TextView) convertView.findViewById(R.id.tv_name);
            holder.mobileTxt = (TextView) convertView.findViewById(R.id.tv_mobile);
            convertView.setTag(holder);

            AbsListView.LayoutParams params =  new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.height = mItemHeight;
            holder.rootLayout.setLayoutParams(params);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String faceUrl = inviteDetailBean.getFaceUrl();
        if (!TextUtils.isEmpty(faceUrl)) {
            BaseApplication.getApplicationInstance().displayRoundImgPixel(faceUrl, holder.faceImg, mContext.getResources().getDimensionPixelOffset(R.dimen.common_dimen_dp70));
        }

        String status = inviteDetailBean.getInviteeStatus().getInviteeStatusMsg();
        String name = inviteDetailBean.getInviteeName();
        String mobile = inviteDetailBean.getInviteeMobile();
        int code = inviteDetailBean.getInviteeStatus().getInviteeStatusCode();

        switch (code) {
            case 0:  //待审核
                holder.faceImg.setColorFilter(mContext.getResources().getColor(R.color.common_color_invite_cc337cff));
                break;
            case 1:  //待注册
                holder.faceImg.setColorFilter(mContext.getResources().getColor(R.color.common_color_invite_cc96999e));
                name = mContext.getString(R.string.unknown);
                mobile = inviteDetailBean.getInviteeType();
                break;
            case 5:  //待同意
                holder.faceImg.setColorFilter(mContext.getResources().getColor(R.color.common_color_invite_ccf7a82f));
                break;
            case 9:  //未通过
                holder.faceImg.setColorFilter(mContext.getResources().getColor(R.color.common_color_invite_ccff1f1f));
                break;
            case 10: //已注册
                status = "";
                break;
        }

        holder.statusTxt.setText(status);
        holder.nameTxt.setText(name);
        holder.mobileTxt.setText(mobile);

        return convertView;
    }

    private class ViewHolder {
        private LinearLayout rootLayout;
        private ImageView faceImg;
        private TextView statusTxt;
        private TextView nameTxt;
        private TextView mobileTxt;
    }
}